﻿<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="UCCategoryList.ascx.cs"
    Inherits="RedHouse.AdminControl.UserControls.UCCategory" %>
<%@ Register Src="~/UserControls/UCCategoryDetail.ascx" TagName="CategoryDetail"
    TagPrefix="uc" %>
<script type="text/javascript">
    function edit(id) {
        $.ajax({
            type: "POST",
            url: "Category.aspx/EditCategory",
            data: "{'id': '" + id + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                bindData(data);
                $("#CategoryDetail").dialog({ title: "Edit Dish", height: 650, width: 600 });
                $("#ctl00_MainContent_CategoryList_ctl00_btnSave").click(function () {
                    var jsonData = getEditData();
                    var jsonText = JSON.stringify({ category: jsonData });
                    $.ajax({
                        type: "POST",
                        url: "Category.aspx/SaveCategory",
                        data: jsonText,
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (data) {

                        },
                        error: function (data) {

                        }
                    })
                    $("#CategoryDetail").dialog("close");
                    location.reload(true);
                });
            },
            error: function (data) {
                alert(data);
            }
        })
    };

    function deleteCategory(id) {
        if (confirm("Are you sure to delete this category?")) {
            $.ajax({
                type: "POST",
                url: "Category.aspx/DeleteCategory",
                data: "{'id': '" + id + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    alert('delete successfully');
                    location.reload(true);
                },
                error: function (data) {
                    alert('delete successfully');
                }
            })
        }
    };

    var RedHouseCategory = function (id, title, group, lang, imageUrl, des, locationId, order) {
        this.Id = id;
        this.Title = title;
        this.Group = group;
        this.LangId = lang;
        this.Description = des;
        this.ImageUrl = imageUrl;
        this.LocationId = locationId;
        this.DisplayOrder = order;
    }

    function bindData(data) {
        var category = jQuery.parseJSON(data.d);
        $("#ctl00_MainContent_CategoryList_ctl00_txtTitle").val(category.Title);
        $("#ctl00_MainContent_CategoryList_ctl00_cboGroup option").each(function () {
            if ($(this).attr("selected") == "selected") {
                $(this).removeAttr("selected");
            }
            if ($(this).attr("value") == category.Group) {
                $(this).attr("selected", "selected");
            }
        });

        $("#ctl00_MainContent_CategoryList_ctl00_cboLanguage option").each(function () {
            if ($(this).attr("selected") == "selected") {
                $(this).removeAttr("selected");
            }
            if ($(this).attr("value") == category.LangId) {
                $(this).attr("selected", "selected");
            }
        });

        $("#ctl00_MainContent_CategoryList_ctl00_cboLocation option").each(function () {
            if ($(this).attr("selected") == "selected") {
                $(this).removeAttr("selected");
            }
            if ($(this).attr("value") == category.LocationId) {
                $(this).attr("selected", "selected");
            }
        });

        $("#ctl00_MainContent_CategoryList_ctl00_txtDescription").val(category.Description);
        $("#ctl00_MainContent_CategoryList_ctl00_txtImageUrl").val(category.ImageUrl);
        $("#ctl00_MainContent_CategoryList_ctl00_hdfId").val(category.Id);
        $("#ctl00_MainContent_CategoryList_ctl00_txtDisplayOrder").val(category.DisplayOrder);
    }

    function getEditData() {

        var title = $("#ctl00_MainContent_CategoryList_ctl00_txtTitle").val();
        var group = "";
        $("#ctl00_MainContent_CategoryList_ctl00_cboGroup option").each(function () {
            if ($(this).attr("selected") == "selected") {
                group = $(this).val();
            }
        });
        var lang = "";
        $("#ctl00_MainContent_CategoryList_ctl00_cboLanguage option").each(function () {
            if ($(this).attr("selected") == "selected") {
                lang = $(this).val();
            }
        });

        var locationId = "";
        $("#ctl00_MainContent_CategoryList_ctl00_cboLocation option").each(function () {
            if ($(this).attr("selected") == "selected") {
                locationId = $(this).val();
            }
        });

        var imageUrl = $("#ctl00_MainContent_CategoryList_ctl00_txtImageUrl").val();
        var des = $("#ctl00_MainContent_CategoryList_ctl00_txtDescription").val();
        var id = $("#ctl00_MainContent_CategoryList_ctl00_hdfId").val();
        var order = $("#ctl00_MainContent_CategoryList_ctl00_txtDisplayOrder").val();

        var jsonData = new RedHouseCategory(id, title, group, lang, imageUrl, des, locationId, order);

        return jsonData;
    }

</script>
<style type="text/css">
    .css-edit
    {
    }
</style>
<div id="CategoryDetail" style="display: none">
    <uc:CategoryDetail runat="server" />
</div>
<br />
<div class="input_field">
    <label for="dropdown">
        Group:
    </label>
    <asp:DropDownList CssClass="dropdown" ID="cboGroup" runat="server" AutoPostBack="true" EnableViewState="true" />
</div>
<br />
<asp:Repeater runat="server" ID="rptCategory">
    <HeaderTemplate>
        <table cellspacing="0" cellpadding="0" border="0">
            <thead>
                <tr>
                    <th>
                        <input type="checkbox" class="checkall" />
                    </th>
                    <%-- <th>
                        No
                    </th>--%>
                    <th>
                        Category Name
                    </th>
                    <th>
                        Description
                    </th>
                    <th>
                        Group
                    </th>
                    <th>
                        Date
                    </th>
                    <th width="70">
                        Actions
                    </th>
                </tr>
            </thead>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td>
                <input type="checkbox" />
            </td>
            <%--  <td>
                <%#DataBinder.Eval(Container.DataItem,"Index")%>
            </td>--%>
            <td>
                <%#DataBinder.Eval(Container.DataItem,"Title")%>
            </td>
            <td>
                <%#DataBinder.Eval(Container.DataItem,"Description")%>
            </td>
            <td>
                <%#DataBinder.Eval(Container.DataItem,"Group")%>
            </td>
            <td>
                <%#DataBinder.Eval(Container.DataItem,"CreatedDate")%>
            </td>
            <td>
                <a href="#"><a href="#">
                    <img src="../assets/action_delete.png" alt="Delete" onclick="javascript:deleteCategory('<%#DataBinder.Eval(Container.DataItem,"Id")%>');" /></a>
                    <a href="#" onclick="javascript:edit('<%#DataBinder.Eval(Container.DataItem,"ID")%>');">
                        <img class="css-edit" src="../assets/action_edit.png" alt="edit" /></a> <a title="Image Gallery"
                            href="Gallery.aspx?id=<%#DataBinder.Eval(Container.DataItem,"ID")%>&category=<%#DataBinder.Eval(Container.DataItem,"Title")%>">
                            <img src="../assets/action_gallery.png" alt="Delete" /></a>
            </td>
        </tr>
    </ItemTemplate>
    <FooterTemplate>
        </tbody> </table>
    </FooterTemplate>
</asp:Repeater>
